package org.bigk.invoices.services;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.bigk.invoices.exceptions.ServiceException;
import org.bigk.invoices.model.Tax;
import org.bigk.invoices.utils.HibernateUtils;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Order;

public class TaxesServiceImpl implements TaxesService {
	/**
	 * Logger for this class
	 */
	private static final Log logger =
		LogFactory.getLog(TaxesServiceImpl.class);

	public TaxesServiceImpl() {
	}

	@SuppressWarnings("unchecked")
	public List<Tax> listAllItems() throws ServiceException {
		if (logger.isDebugEnabled()) {
			logger.debug("listAllItems() - start");
		}

		List<Tax> list = null;
		Session session = null;
		try {
			session = HibernateUtils.getCurrentSession();
			Criteria crit = session.createCriteria(Tax.class);
			crit.addOrder(Order.asc("id"));
			list = crit.list();
			
		} catch (HibernateException ex) {
			logger.error("listAllItems()", ex);
			throw new ServiceException(ex);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("listAllItems() - end - return value=" + CollectionUtils.size(list));
		}
		return list;
	}
	
	public Tax getTax(Long id) throws ServiceException {
		if (logger.isDebugEnabled()) {
			logger.debug("getTax(Long id=" + id + ") - start");
		}

		Tax object = null;
		Session session = null;
		try {
			session = HibernateUtils.getCurrentSession();
			object = (Tax) session.get(Tax.class, id);
			
		} catch (HibernateException ex) {
			logger.error("getTax(Long)", ex);
			throw new ServiceException(ex);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("getTax(Long) - end - return value=" + object);
		}
		return object;
	}
}
